package com.cloud.punish.tasklist.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cloud.punish.common.result.QkIcon;
import com.cloud.punish.common.result.QkTreeNode;
import com.cloud.punish.common.utils.SqlParserHelper;
import com.cloud.punish.common.utils.TreeHelper;
import com.cloud.punish.tasklist.entity.TaskList;
import com.cloud.punish.tasklist.entity.TaskListField;
import com.cloud.punish.tasklist.entity.TaskListGroup;
import com.cloud.punish.tasklist.mapper.TaskListFieldMapper;
import com.cloud.punish.tasklist.mapper.TaskListGroupMapper;
import com.cloud.punish.tasklist.mapper.TaskListMapper;
import com.cloud.punish.tasklist.service.TaskListConfigService;

import tk.mybatis.mapper.entity.Example;

@Service
@Transactional
public class TaskListConfigServiceImpl implements TaskListConfigService {

	@Autowired
	private TaskListMapper taskListMapper;

	@Autowired
	private TaskListFieldMapper taskListFieldMapper;

	@Autowired
	private TaskListGroupMapper taskListGroupMapper;

	@Autowired
	private JdbcTemplate jdbcTemplate;

	@Override
	public QkTreeNode[] listTaskListTree() {
		List<TaskList> taskLists = taskListMapper.listTaskListTree();
		List<QkTreeNode> nodes = new ArrayList<QkTreeNode>();
		List<Integer> groups = new ArrayList<Integer>();
		for (TaskList taskList : taskLists) {
			if (!groups.contains(taskList.getGroupId())) {
				QkTreeNode groupNode = new QkTreeNode("G_" + taskList.getGroupId(), taskList.getGroupName());
				groupNode.setIcon(QkIcon.FA_FOLDER_OPEN);
				groupNode.setSelectable(false);
				groupNode.setExpanded(true);
				groupNode.setRootFlag(true);
				nodes.add(groupNode);
				groups.add(taskList.getGroupId());
			}
			QkTreeNode taskListNode = new QkTreeNode(taskList.getTaskListId(), taskList.getTaskListName());
			taskListNode.setIcon(QkIcon.FA_LAPTOP);
			taskListNode.setSelectable(true);
			taskListNode.setLeaf(true);
			taskListNode.setSeniorId("G_" + taskList.getGroupId());
			nodes.add(taskListNode);

		}
		return TreeHelper.buildList2Tree(nodes);
	}

	@Override
	public TaskList showDetail(long taskListId) {
		return taskListMapper.selectByPrimaryKey(taskListId);
	}

	@Override
	public Long saveUpdateTaskList(TaskList taskList) {
		// SQL语法校验
		List<TaskListField> fields = SqlParserHelper.listFieldInfoBySQL(taskList.getTaskListSql(), jdbcTemplate);
		// 保存案卷箱子
		Long taskListId = taskList.getTaskListId();
		if (null == taskListId || taskListId.intValue() == 0) {
			taskListMapper.insert(taskList);
			for (TaskListField field : fields) {
				field.setTaskListId(taskListId);
				field.setFieldWidth(200);
				taskListFieldMapper.insert(field);
			}
		} else {
			TaskList tl = taskListMapper.selectByPrimaryKey(taskListId);
			if (StringUtils.isEmpty(tl.getTaskListSql()) || !tl.getTaskListSql().equals(taskList.getTaskListSql())) {
				// SQL修改，需重新生成字段
				List<String> fieldNames = new ArrayList<String>();
				List<TaskListField> needInfer = new ArrayList<TaskListField>();
				for (TaskListField f : fields) {
					fieldNames.add(f.getColumnName());
					f.setTaskListId(taskListId);
					if (!taskListFieldMapper.existsWithPrimaryKey(f)) {
						f.setFieldWidth(200);
						taskListFieldMapper.insert(f);
						needInfer.add(f);
					}
				}
				// 刪除減少的列
				Example fieldsExample = new Example(TaskListField.class);
				Example.Criteria criteria = fieldsExample.createCriteria();
				criteria.andEqualTo("taskListId", taskListId);
				criteria.andNotIn("columnName", fieldNames);
				taskListFieldMapper.deleteByExample(fieldsExample);

				// TODO:根据已经配置的数据推断新增加的列的各种情况信息
				if (needInfer.size() > 0) {

				}
			}

			taskListMapper.updateByPrimaryKey(taskList);
		}
		return taskListId;
	}

	@Override
	public Long delete(long taskListId) {
		// 刪除栏目字段
		Example tlfExample = new Example(TaskListField.class);
		Example.Criteria tlfCriteria = tlfExample.createCriteria();
		tlfCriteria.andEqualTo("taskListId", taskListId);
		taskListFieldMapper.deleteByExample(tlfExample);
		//删除栏目列表权限
		jdbcTemplate.update("delete from task_list_part where task_list_id = ?", taskListId);
		//删除栏目菜单权限
		jdbcTemplate.update("delete from task_list_menu_part where task_list_id = ?", taskListId);
		//删除栏目标签权限
		jdbcTemplate.update("delete from task_list_tab_part where task_list_id = ?", taskListId);
		// 删除栏目
		taskListMapper.deleteByPrimaryKey(taskListId);
		//删除分组
		jdbcTemplate.update("delete from task_list_group where group_id not in (select group_id from task_list)");
		return taskListId;
	}

	@Override
	public TaskList addTaskList(TaskList taskList) {
		int groupId = taskList.getGroupId();
		if (0 == groupId) {
			String groupName = taskList.getGroupName();
			Example example = new Example(TaskListGroup.class);
			Example.Criteria criteria = example.createCriteria();
			criteria.andEqualTo("groupName", groupName);
			List<TaskListGroup> groups = taskListGroupMapper.selectByExample(example);
			if (null == groups || groups.size() == 0) {
				TaskListGroup group = new TaskListGroup();
				group.setGroupName(groupName);
				group.setDisplayOrder(groupId);
				taskListGroupMapper.insert(group);
				groupId = group.getGroupId();
			}else{
				groupId = groups.get(0).getGroupId();
			}
		}
		taskList.setGroupId(groupId);
		taskListMapper.insert(taskList);
		return taskList;
	}

	@Override
	public List<TaskListGroup> listGroup() {
		return taskListGroupMapper.selectAll();
	}

}
